package com.jiubanqingchen.org.user;

import com.alibaba.excel.EasyExcel;
import com.jfinal.aop.Clear;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.jiubanqingchen.framework.annotation.AuthorityKey;
import com.jiubanqingchen.framework.base.controller.ModelController;
import com.jiubanqingchen.framework.context.locale.LocaleContextHolder;
import com.jiubanqingchen.framework.exception.WishExceptionKey;
import com.jiubanqingchen.interceptors.AuthorityInterceptor;
import com.jiubanqingchen.interceptors.LoginInterceptor;
import com.jiubanqingchen.kit.I18nKit;
import com.jiubanqingchen.model.models.User;
import com.jiubanqingchen.plugin.easyexcel.listener.MapDataListener;

import java.util.Locale;

/**
 * @author light_dust
 * @since 2020/12/27 20:11
 */
@Path("/org/user")
@AuthorityKey("org:user")
public class UserController extends ModelController<UserService, User> {

    @Clear(AuthorityInterceptor.class)
    public void info() {
        renderJson(service.info(getCurrentUser()));
    }

    @AuthorityKey("upload")
    public void uploadExcel() {
        UploadFile uploadFile = getFile();
        String socketId = getPara("socketId");
        MapDataListener mapDataListener = new MapDataListener(socketId);
        EasyExcel.read(uploadFile.getFile(), mapDataListener).sheet().doRead();
        service.uploadExcel(uploadFile, getCurrentUser());
        renderJson(true);
    }

    @Clear({LoginInterceptor.class, AuthorityInterceptor.class})
    public void test() {
        Locale locale = LocaleContextHolder.getLocale();
        System.out.println(locale.toString());
        System.out.println(locale.getLanguage());
        System.out.println(locale.getCountry());
        Record record = new Record();
        record.set(WishExceptionKey.MODEL_CREATE_DAO_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_CREATE_DAO_ERROR, "UserService"));
        record.set(WishExceptionKey.MODEL_INSERT_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_INSERT_ERROR));
        record.set(WishExceptionKey.MODEL_UPDATE_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_UPDATE_ERROR));
        record.set(WishExceptionKey.MODEL_OPERATE_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_OPERATE_ERROR));
        record.set(WishExceptionKey.MODEL_ID_IS_NULL_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_ID_IS_NULL_ERROR));
        record.set(WishExceptionKey.MODEL_ID_INCONSISTENT_NUMBER_ERROR, I18nKit.getLocalMessage(WishExceptionKey.MODEL_ID_INCONSISTENT_NUMBER_ERROR));
        renderJson(record);
    }

    @Clear({LoginInterceptor.class, AuthorityInterceptor.class})
    public void test2() {
        Locale locale = LocaleContextHolder.getLocale();
        System.out.println(locale.toString());
        System.out.println(locale.getLanguage());
        System.out.println(locale.getCountry());
    }
}
